package utility;
import java.sql.*;

public class DataBaseServerSystem {
	private Connection conn = null;
	private Statement stm = null;
	private ResultSet rs = null;
	
	
	public DataBaseServerSystem(String accName, String password){
		
		init(accName, password);
	}
	
	private void init(String accName, String password){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?"+"user="+accName+"&password="+password);
			stm = conn.createStatement();
			
		}catch(ClassNotFoundException e){
			System.out.println("Can not found Mysql driver");
			System.exit(1);
		}catch(SQLException e){
			System.out.println("Can not connect to Mysql");
			System.exit(1);
		}
	}
	
	public void showTablesDetail(){
		try {
			rs = stm.executeQuery("select * from mytest;");
			while(rs.next()){
				System.out.println(rs.getString("id"));
				System.out.println(rs.getString("name"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void closeDB(){
		try{
			if(rs != null){
				rs.close();
				rs = null;
			}
			if(stm != null){
				stm.close();
				stm = null;
			}
			if(conn != null){
				conn.close();
				conn = null;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
